package com.yangtzeu.jwc.app;

import java.io.File;
import java.io.FileNotFoundException;

import android.os.Environment;

import com.yangtzeu.jwc.utils.G;

public class MyStorage {

    /**
     * 判断目录是否存在，不在则创建
     */
    public static void isExistDirectory(String directoryName) {
        File file = new File(directoryName);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    /**
     * 获得SD目录路径
     */
    public static String getSdCardPath() {
        return Environment.getExternalStorageDirectory().getPath();
    }

    /**
     * 判断文件是否存在
     */
    public static boolean isExistFile(String file) {
        return new File(file).exists();
    }

    /**
     * 检查SD卡是否已装载
     */
    public static boolean isExistSdCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 根据文件名获取不带后缀名的文件名
     */
    public static String clearSuffix(String str) {
        int i = str.lastIndexOf(".");
        if (i != -1) {
            return str.substring(0, i);
        }
        return str;
    }

    public static String getFileNameByPath(String path) {
        String fileName = null;
        int i = path.lastIndexOf("/");
        if (i != -1) {
            fileName = path.substring(i + 1, path.length());
        }
        return fileName;
    }

    public static File getSDFile(String fileName) throws FileNotFoundException {
        File sd = getSDCard();
        if (sd == null) {
            throw new FileNotFoundException("SD卡不存在");
        } else {
            return new File(sd, fileName);
        }
    }

    public static File getSDCard() {
        return Environment.getExternalStorageDirectory();
    }

    /**
     * 获取文件的后缀名，返回小写
     */
    public static String getSuffix(String str) {
        int i = str.lastIndexOf('.');
        if (i != -1) {
            return str.substring(i + 1).toLowerCase();
        }
        return str;
    }

    public static File getMyFolder() throws FileNotFoundException {
        if (!isExistSdCard()) {
            throw new FileNotFoundException("SD卡不存在");
        } else {
            String mPath = getSdCardPath() + G.MY_FOLDER;
            File mFolder = new File(mPath);
            if (!mFolder.exists()) {
                mFolder.mkdirs();
            }
            return mFolder;
        }
    }

    public static File getUpdateFolder() throws FileNotFoundException {
        if (!isExistSdCard()) {
            throw new FileNotFoundException("SD卡不存在");
        } else {
            String mPath = getSdCardPath() + G.UPADATE_FOLDER;
            File mFolder = new File(mPath);
            if (!mFolder.exists()) {
                mFolder.mkdirs();
            }
            return mFolder;
        }
    }
}
